package com.ecoolex.discount.sales.admin.graphql;

import com.ecoolex.discount.sales.admin.core.AdminConstant;
import com.ecoolex.discount.sales.entity.DiscountBrandWithBLOBs;
import com.ecoolex.discount.sales.entity.DiscountOrder;
import com.ecoolex.discount.sales.entity.DiscountOrderDetail;
import com.ecoolex.discount.sales.entity.DiscountOrderRefund;
import com.ecoolex.discount.sales.entity.DiscountProductWithBLOBs;
import com.ecoolex.discount.sales.enums.DiscountOrderStatus;
import com.ecoolex.discount.sales.pojo.QueryDiscountOrderDTO;
import com.ecoolex.discount.sales.ws.DiscountOrderService;
import com.ecoolex.discount.sales.ws.DiscountProductService;
import com.ecoolex.discount.sales.ws.PaymentOrderService;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import java.util.List;

@GraphQL2Module(type = GraphQL2Module.Type.Admin,description = "折扣订单")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_PROJECT_KEY,
        key = DiscountOrderGraphql.AUTH,
        name = "折扣订单管理",
        remark = "折扣订单管理")
public class DiscountOrderGraphql {

    public static final String AUTH = AdminConstant.AUTH_PROJECT_KEY + "-DISCOUNT-ORDER";

    public static final String AUTH_GET = AUTH + "-GET";
    public static final String AUTH_QUERY = AUTH + "-QUERY";
    public static final String AUTH_REFUND = AUTH + "-REFUND";

    @Reference
    private DiscountOrderService discountOrderService;
    @Reference
    private PaymentOrderService paymentOrderService;
    @Reference
    private DiscountProductService discountProductService;

    @TypeField(type = DiscountOrder.class, field = "canRefund", description = "是否能退款")
    public boolean canRefund(@Source DiscountOrder source) {
        return DiscountOrderStatus.canRefund(source);
    }
    
    @TypeField(type = DiscountOrder.class, field = "discountOrderRefund", description = "最新一条退款单")
    public DiscountOrderRefund discountOrderRefund(@Source DiscountOrder source) {
    	return discountOrderService.getByOrderId(source.getOrderId());
    }

    @TypeField(type = DiscountOrder.class, field = "detailList", description = "订单详细列表")
    public List<DiscountOrderDetail> detailList(@Source DiscountOrder source) {
        return discountOrderService.queryOrderDetailByOrderId(source.getOrderId());
    }

    @TypeField(type = DiscountOrder.class, field = "brand", description = "折扣品牌")
    public DiscountBrandWithBLOBs brand(@Source DiscountOrder source) {
        return discountProductService.getDiscountBrand(source.getBrandId());
    }

    @TypeField(type = DiscountOrder.class, field = "product", description = "折扣产品")
    public DiscountProductWithBLOBs product(@Source DiscountOrder source) {
        return discountProductService.getProductById(source.getProductId());
    }


    @QueryField(field = "discountOrder", description = "获取折扣订单详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取折扣订单详情")
    public DiscountOrder get(@NonNull Integer orderId,  AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return discountOrderService.getOrderById(orderId);
    }

    @QueryField(field = "discountOrderList", description = "查询折扣订单列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询折扣订单列表")
    public Pagination<DiscountOrder> query(@NonNull  QueryDiscountOrderDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return discountOrderService.query(query, sorts, page);
    }

    @MutationField(field = "refundDiscountOrder", description = "折扣订单退款")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_REFUND, name = "折扣订单退款")
    public boolean refund(@NonNull Integer orderId,  AdminMemberAuthority user) {
        user.assertAuth(AUTH_REFUND);
        //discountOrderService.refund(orderId, user.userName());
        return false;
    }
}
